//
//  PassWord.m
//  AboutExcy
//
//  Created by apple on 12-8-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "PassWord.h"
#import "MyMD5.h"
#import "GTMBase64.h"
@implementation PassWord
+ (NSString*)encodeBase64:(NSString*)input
{
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    //转换到base64
    data = [GTMBase64 encodeData:data];
    NSString * base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return [base64String autorelease];
}
// 对字符串进行解码
+(NSString*)decodeBase64:(NSString*)input
{
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    //转换到base64
    data = [GTMBase64 decodeData:data];
    NSString * base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return [base64String autorelease];
}




+(NSString *)dePassWord:(NSString *)passWordt dlegate:(id)delegate
{
//    NSString *passWord=@"YwyfwyfFe5X-tN=iYtcTiYtce5X=9f";
//    
//    NSString *secrectString=@"BCNOPyst=lAzgDEFhi_jQRST-UwxkVWabcXYZdef+IJK67nopqr89LMmGH01254/3uv";
    NSString *passWord = passWordStr;
//    NSString *secrectString=@"BCNOPystlAzgDEFhijQRSTUwxkVWabcXYZdefIJK67nopqr89LMmGH012543uv=";
    NSString *secrectString= lockStreamStr;

    // 取密码本的长度
   
    // 传进来的字符串的长度
    
    int lengeth1=passWordt.length;
    
    // 取最后一个字符
    NSString *end=[passWordt substringFromIndex:lengeth1-1];
    
    // 密码加尾字符
    
    passWord=[passWord stringByAppendingString:end];
    
    passWord=[[MyMD5 md5:passWord]uppercaseString];
    
    // 取最后字符在密码本的位置
    
    int teger=[secrectString rangeOfString:end].location;
    
    // 去掉尾字符
    passWordt=[passWordt substringToIndex:passWordt.length-1];
    
    // 初始化工作
    NSMutableString *str=[[NSMutableString alloc]init];
    
    int i=0,j=0,k=0;
    
    for(i=0;i<passWordt.length;i++)
    {
       if(k==passWord.length)
       {
           k=0;
       }   
    
        j=[secrectString rangeOfString:[passWordt substringWithRange:NSMakeRange(i, 1)]].location-teger-(int)[[passWord substringWithRange:NSMakeRange(k, 1)]characterAtIndex:0];
               
        while (j<0)
        {
            j=j+[secrectString length];
        }
        [str appendString:[secrectString substringWithRange:NSMakeRange(j, 1)]] ;
        k++;
    }
    
    NSString *str1=[self decodeBase64:str];
    [str release];
    
    return str1;
}

+(NSString *)enPassWord:(NSString *)passWordt dlegate:(id)delegate
{
//    NSString *passWord=@"YwyfwyfFe5X-tN=iYtcTiYtce5X=9f";
//    
//    NSString *lockStream=@"BCNOPyst=lAzgDEFhi_jQRST-UwxkVWabcXYZdef+IJK67nopqr89LMmGH01254/3uv";
    NSString *passWord = passWordStr;
//    NSString *lockStream=@"BCNOPystlAzgDEFhijQRSTUwxkVWabcXYZdefIJK67nopqr89LMmGH012543uv=";
    NSString *lockStream= lockStreamStr;
    
    int locklen=lockStream.length;
    
    int lockcount=arc4random()%locklen;
    
    NSString *randomLock=[lockStream substringWithRange:NSMakeRange(lockcount, 1)];
    
    passWord=[passWord stringByAppendingString:randomLock];
    
    passWord=[[MyMD5 md5:passWord] uppercaseString];
    
    passWordt=[self encodeBase64:passWordt];
    
    NSMutableString *tempString=[[[NSMutableString alloc]init]autorelease];
    
    int i=0,j=0,k=0;
    
    for(i=0;i<passWordt.length;i++)
    {
        if(k==passWord.length)
        {
            k=0;
            
        }   
        
        j= ([lockStream rangeOfString:[passWordt substringWithRange:NSMakeRange(i, 1)]].location+lockcount+(int)[[passWord substringWithRange:NSMakeRange(k, 1) ] characterAtIndex:0])%locklen;

       [tempString appendString:[lockStream substringWithRange:NSMakeRange(j, 1)]];
    
        k++;
    }
    
    [tempString appendString:randomLock];
    
    return tempString;
    
    
    
/*
 NSMutableArray *Base64Arr=[[NSMutableArray alloc]init];
    
 NSMutableArray *MD5Arr=[[NSMutableArray alloc]init];
    
  int   first=0;
    
  int   second=0;
    
   int  third=0;

   int  really=0;
    
  NSString *secrectString1=[[NSString alloc]init];
    
    // 进行对内容进行URL编码
    NSString * strURL = passWordt;
    
    // 内容的密码 
    

   // NSString *passWord=dataCopy.password;
    
   // NSArray *secrecArr=dataCopy.dataArr;
    
   // NSString *passWord=@"YwyfwyfFe5X-tN=iYtcTiYtce5X=9f";
    
    NSString *passWord=@"Ywy";
    
    NSString *secrectString=@"B|C|N|O|P|y|s|t|=|l|A|z|g|D|E|F|h|i|_|j|Q|R|S|T|-|U|w|x|k|V|W|a|b|c|X|Y|Z|d|e|f|+|I|J|K|6|7|n|o|p|q|r|8|9|L|M|m|G|H|0|1|2|5|4|/|3|u|v";
    
    NSArray *secrecArr=[secrectString componentsSeparatedByString:@"|" ];
    

    
    
    
    

    // 随机取一个字符 
    second=abs(arc4random());
    
    NSString *t=[secrecArr objectAtIndex:second%[secrecArr count]];
    
    //  取出的字符 与原密码 进行 想加之后进行MD5加密 
    
    NSString *newPassWord=[passWord stringByAppendingString:t];
    
    NSString *newMD5Word=[[MyMD5 md5:newPassWord] uppercaseString];
    
    int md5Lenth=[newMD5Word length];
    
    for(int i=0;i<md5Lenth;i++)
    {
        NSString *sub=[newMD5Word substringWithRange:NSMakeRange(i, 1)];
        
        
        [MD5Arr addObject:sub];
        
        [sub release];
        
        
    }  
    
    
    
    
    // 开始对你的内容进行加密 也就是 进行了URL编码后的字符串进行加密
    
    // 第一步 对你 url编码后的进行base64 编码
    
    NSString *firstSecrectWord=[PassWord encodeBase64:strURL];
    
   
    
    // base64 编码后的长度
    int lenth=[firstSecrectWord length];
    int mm=0;
    // 将字符串转成 ARR 
    
    // 把字符串 存入数组当中
    for(int i=0;i<lenth;i++)
    {
        
        
        NSString *sub=[firstSecrectWord substringWithRange:NSMakeRange(i, 1)];
        
        [sub characterAtIndex:0];
        
        [Base64Arr addObject:sub];
        
        
    }   
    

    
    
    for(int i=0;i<lenth;i++)
    {
        
        
        
        if(mm==md5Lenth)
            
        {
            
            mm=0;
        }
        
        
        
        NSString *temp=(NSString *)[Base64Arr objectAtIndex:i];
        
        
        
        for( int n=0;n<[secrecArr count];n++)
        {
            
            
            
            if([temp isEqual:[secrecArr objectAtIndex:n]])
            {
                
                first=n;
                
                
                break;
                
            }
            
            
    
            
            
        }
        
        
        
        third=(int)[[MD5Arr objectAtIndex:mm] characterAtIndex:0] ; 
        
        
      
        
        really=(first+second+third)%[secrecArr count];
        
        
        secrectString1= [secrectString1  stringByAppendingString:[secrecArr objectAtIndex:really]];
        
        
        
        mm++;
        

        




    }

    return [secrectString1 stringByAppendingString:t];
 */   
    
}










@end
